/**
 * 
 */
package com.taobao.snowsnow.taobao;

import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.tree.DefaultElement;

import com.taobao.api.ApiException;
import com.taobao.api.request.ItemGetRequest;
import com.taobao.api.request.ItemUpdateRequest;
import com.taobao.api.response.ItemGetResponse;
import com.taobao.api.response.ItemUpdateResponse;

/**
 * @author tux
 *
 */
public class ProductUpdate extends Taobao {
	
	private void getItem(long num_iid) throws ApiException, DocumentException {
		String desc = "";
		
		ItemGetRequest req=new ItemGetRequest();
		req.setFields("desc");
		req.setNumIid(num_iid);
		ItemGetResponse response = client.execute(req , TaoBaoUtil.SESSIONKEY);
		
		Document document = DocumentHelper.parseText(response.getBody());

		@SuppressWarnings("unchecked")
		List<DefaultElement> list = document.selectNodes("//item_get_response/item");

		for (DefaultElement trades_element : list) {
			desc = trades_element.elementText("desc");
			// 在描述中加入一个空格
			desc = desc.replaceFirst("&nbsp;", "&nbsp;&nbsp;");
			System.out.println(desc);
		}
		
		updateItem(num_iid, desc);
	}
	
	private void updateItem(long num_iid	, String desc) throws ApiException {
		ItemUpdateRequest req = new ItemUpdateRequest();
		req.setNumIid(num_iid);
		req.setDesc(desc);
		
		ItemUpdateResponse response = client.execute(req, TaoBaoUtil.SESSIONKEY);
		System.out.println(response.getBody());
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			new ProductUpdate().getItem(12573532251L);
		} catch (ApiException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
